import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";

//压缩代码，开启GZIP
import viteCompression from 'vite-plugin-compression'


// import AutoImport from 'unplugin-auto-import/vite'
import { visualizer } from "rollup-plugin-visualizer";
import path from "path";

const srcPath = path.resolve(__dirname, "./src");
const processData: any = process
//@ts-ignore
export default defineConfig((config) => {
  const envData = loadEnv(config.mode, process.cwd())
  const reg = new RegExp(`${envData.VITE_APP_API_PREFIX}`);
  return {
    server: {
      host: "0.0.0.0",
      port: 8080,
      open: true,
      proxy: {
        [envData.VITE_APP_API_PREFIX]: {
          target: envData.VITE_APP_API_HOST,
          autoRewrite: true,
          cookieDomainRewrite: {
            '*': ''
          },
          rewrite: (path) => path.replace(reg, ''),
          changeOrigin: true
        },
        [envData.VITE_APP_API_PREFIX_YUN]: {
          target: envData.VITE_APP_API_YUN_HOST,
          autoRewrite: true,
          cookieDomainRewrite: {
            '*': ''
          },
          rewrite: (path) => path.replace(envData.VITE_APP_API_PREFIX_YUN, ''),
          changeOrigin: true
        }
      },
      hmr: true,
      usePolling: true

    },
    base: "./",
    build: {
      target: "esnext",
    },
    plugins: [


      //打包体积分析
      //   visualizer({
      //     filename: "dist/stats.html",
      //     title: "visualizer",
      //     open: true,
      //   }),
      vue(),
      vueJsx(),
      viteCompression({
        threshold: 300000 // 对大于 500kb 的文件进行压缩
      })
      //AutoImport({ /* options */ }),
      // topLevelAwait({
      //   // The export name of top-level await promise for each chunk module
      //   promiseExportName: '__tla',
      //   // The function to generate import names of top-level await promise in each chunk module
      //   promiseImportName: i => `__tla_${i}`
      // })
    ],
    resolve: {
      alias: {
        "@": srcPath,
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            'primary-color': '#1f6ded',
            'link-color': '#1f6ded',
            'border-radius-base': '2px',
          },
          javascriptEnabled: true
        }
      }
    },
  }
});

